<?php

function curl_post($post_url, $data = array(), $flag = false) {
    global $http_status_code;
    $intTimeStart = microtime(true);
    $logString = 'Posting:' . $post_url;

    $post = http_build_query($data);
    $login = curl_init($post_url); //创建CURL对象  
    curl_setopt($login, CURLOPT_HEADER, 0); //返回头部  
    curl_setopt($login, CURLOPT_RETURNTRANSFER, 1); //返回信息  
    curl_setopt($login, CURLOPT_POST, 1); //设置POST提交  
    curl_setopt($login, CURLOPT_POSTFIELDS, $post); //提交POST数据  
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($login, CURLOPT_TIMEOUT, 30);

    curl_setopt($login, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($login, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在

    $data = curl_exec($login); //执行已经定义的设置  

    $info = curl_getinfo($login);
    $httpCode = intval($info['http_code']);
    $http_status_code = $httpCode;

    if ($curl_errno = curl_errno($login)) {
        log::w('curl_errno[' . $curl_errno . '],curl_error[' . curl_error($login) . ']');
        if (!$flag) {
            log::w('sleep 5 and retrying...:');
            sleep(5);
            return curl_post($post_url, $data, true);
        }
    }
    curl_close($login); //关闭 */ 
    $intTimeEnd = microtime(true);
    log::n('HttpStatusCode:' . $httpCode . '|RequestTime:' . ($intTimeEnd - $intTimeStart) . 's|' . $logString);
    #log::n('CurlErrno:' . $curl_errno . '|RequestTime:' . ($intTimeEnd - $intTimeStart) . 's|' . $logString);
    return $data;
}

?>